#include "widget.h"
#include "ui_widget.h"
#include <QStateMachine>
#include <QState>
#include <QFinalState>
#include <QHistoryState>
#include <QMessageBox>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    //testhistory();
    //testparral();
    testfinished();
}

Widget::~Widget()
{
    delete ui;
}

//测试historystate
void Widget::testhistory()
{
    QStateMachine *machine=new QStateMachine;

    QState* state1=new QState();
    QState* state2=new QState();
    QState* state11=new QState(state1);
    QState* state12=new QState(state1);
//指定属性值
    state11->assignProperty(ui->label1,"text","state11");
//指定状态变化
    state11->addTransition(ui->pushButton1,SIGNAL(clicked()),state12);

    state12->assignProperty(ui->label1,"text","state12");
    state12->addTransition(ui->pushButton1,SIGNAL(clicked()),state11);


    state1->setInitialState(state11);
    state1->addTransition(ui->pushButton2,SIGNAL(clicked()),state2);
    machine->addState(state1);
    machine->setInitialState(state1);


    state2->assignProperty(ui->label1,"text","state2");
    machine->addState(state2);

    QMessageBox *messagebox=new QMessageBox;
    messagebox->addButton(QMessageBox::Ok);
    messagebox->setText("Message");
    connect(state2,SIGNAL(entered()),messagebox,SLOT(exec()));

    //historystate 记录父state
    QHistoryState * historystate=new QHistoryState(state1);
    //state2会自动转移到historystate
    state2->addTransition(historystate);

    machine->start();
}
//测试Parallel模式，和Exclusive的区别在于，子State是并行的，互不干扰
void Widget::testparral()
{
    QStateMachine *machine=new QStateMachine;

    QState* state0=new QState;
    machine->addState(state0);

    QState* state1=new QState(QState::ParallelStates); //state11和state12互不干扰
    machine->addState(state1);

    state0->addTransition(ui->pushButton3,SIGNAL(clicked()),state1);
    state1->addTransition(ui->pushButton3,SIGNAL(clicked()),state0);

    QState* state11=new QState(state1);
    QState* state12=new QState(state1);

    QState* clean=new QState(state11);
    QState* noclean=new QState(state11);
    state11->setInitialState(clean);

    QState* move=new QState(state12);
    QState* nomove=new QState(state12);
    state12->setInitialState(move);

    move->assignProperty(ui->label1,"text","move");
    nomove->assignProperty(ui->label1,"text","nomove");
    move->addTransition(ui->pushButton1,SIGNAL(clicked()),nomove);
    nomove->addTransition(ui->pushButton1,SIGNAL(clicked()),move);

    clean->assignProperty(ui->label2,"text","clean");
    noclean->assignProperty(ui->label2,"text","noclean");
    clean->addTransition(ui->pushButton2,SIGNAL(clicked()),noclean);
    noclean->addTransition(ui->pushButton2,SIGNAL(clicked()),clean);

    machine->setInitialState(state0);
    machine->start();
}
//当子state中finalstate时，父state会发出finished信号
void Widget::testfinished()
{
    QStateMachine* machine=new QStateMachine;
    QState* state1=new QState;
    QState* state2=new QState;
    machine->addState(state1);
    machine->addState(state2);
    state2->assignProperty(ui->label1,"text","instate2");
    machine->setInitialState(state1);

    QState* state11=new QState(state1);
    QFinalState *finalstate=new QFinalState(state1);
    state1->setInitialState(state11);

    //state11会进入final state从而导致进入state2
    state11->addTransition(ui->pushButton1,SIGNAL(clicked()),finalstate);
    state1->addTransition(state1,SIGNAL(finished()),state2);

    machine->start();
}

void Widget::testtargetless()
{

}
